package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.constant.HttpConstant;
import com.copyctrl.campus.common.constant.RedisConstant;
import com.copyctrl.campus.common.constant.Status;
import com.copyctrl.campus.common.domain.User;
import com.copyctrl.campus.common.exception.DataBaseException;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.service.FollowService;
import com.copyctrl.model.CampFollow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    FollowService followService;

    @Autowired
    StringRedisTemplate redisTemplate;
    @PostMapping("/{follow}")
    public ApiResponse guanzhu(@PathVariable String follow){
        CampFollow bean = JSONUtil.toBean(follow, CampFollow.class);
        int follow1 = followService.follow(bean);
        if (follow1==0){
            throw new DataBaseException(Status.UNKNOWN_ERROR);
        }
        return ApiResponse.ofSuccess(null);
    }

    @DeleteMapping("/{follow}")
    public ApiResponse quguan(@PathVariable String follow){
        CampFollow bean = JSONUtil.toBean(follow, CampFollow.class);
        int follow1 = followService.delete(bean);
        if (follow1==0){
            throw new DataBaseException(Status.UNKNOWN_ERROR);
        }
        return ApiResponse.ofSuccess(null);
    }
    @GetMapping
    public ApiResponse getCount(HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        String json = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(json, User.class);
        //粉丝数量
        Long size = redisTemplate.opsForSet().size(RedisConstant.USER_IS_FOLLOWED+user.getId());
        //关注数量
        Long size1 = redisTemplate.opsForSet().size(RedisConstant.USER_FOLLOW+user.getId());
        //好友数量
        Long size2 = redisTemplate.opsForSet().size(RedisConstant.USER_FRIEND + user.getId());
        Map result=new HashMap();
        result.put("follow",size);
        result.put("attention",size1);
        result.put("friend",size2);
        return ApiResponse.ofSuccess(result);
    }

    @GetMapping("/followers")
    public ApiResponse getAllFollowers(HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        String userJson = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(userJson, User.class);
        List<User> allFollowers = followService.findAllFollowers(user.getId());
        return ApiResponse.ofSuccess(allFollowers);
    }

    @GetMapping("/attention")
    public ApiResponse getAllAttentions(HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        String userJson = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(userJson, User.class);
        List<User> allFollowers = followService.findAllAttentions(user.getId());
        return ApiResponse.ofSuccess(allFollowers);
    }

    @GetMapping("/{id}")
    public ApiResponse getCountById(@PathVariable String id,HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        String userJson = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(userJson, User.class);
        //粉丝数量
        Long size = redisTemplate.opsForSet().size(RedisConstant.USER_IS_FOLLOWED+id);
        //关注数量
        Long size1 = redisTemplate.opsForSet().size(RedisConstant.USER_FOLLOW+id);
        Set<String> union = redisTemplate.opsForSet().intersect(RedisConstant.USER_FRIEND + id, RedisConstant.USER_FRIEND + user.getId());
        Map result=new HashMap();
        result.put("follow",size);
        result.put("attention",size1);
        result.put("friend",union);
        return ApiResponse.ofSuccess(result);
    }

    //看是否关注
    @GetMapping("/ed/{id}")
    public ApiResponse isFollowedOrNot(@PathVariable String id,HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        String userJson = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(userJson, User.class);
        Set<String> members = redisTemplate.opsForSet().members(RedisConstant.USER_FOLLOW + user.getId());
        boolean contains = members.contains(id);
        return ApiResponse.ofSuccess(contains);
    }

    @GetMapping("/friends")
    public ApiResponse getAllFriends(HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        String userJson = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(userJson, User.class);
        List<User> allFollowers = followService.findAllFollowers(user.getId());
        return ApiResponse.ofSuccess(allFollowers);
    }
}
